﻿// CheckCaseNewDialog.cpp: 实现文件
//

#include "stdafx.h"
#include "zChecker.h"
#include "CheckCaseNewDialog.h"
#include "afxdialogex.h"


// CCheckCaseNewDialog 对话框

IMPLEMENT_DYNAMIC(CCheckCaseNewDialog, CDialogEx)

CCheckCaseNewDialog::CCheckCaseNewDialog(CWnd* pParent /*=nullptr*/)
	: CDialogEx(IDD_DIALOG_CHECK_CASE_NEW, pParent)
	, m_strCaseName(_T(""))
{

}

CCheckCaseNewDialog::~CCheckCaseNewDialog()
{
}

void CCheckCaseNewDialog::DoDataExchange(CDataExchange* pDX)
{
	CDialogEx::DoDataExchange(pDX);
	DDX_Text(pDX, IDC_EDIT_CASE_NAME, m_strCaseName);
	DDX_Control(pDX, IDC_COMBO_MODEL, m_cmbModels);
}


BEGIN_MESSAGE_MAP(CCheckCaseNewDialog, CDialogEx)
	ON_BN_CLICKED(IDOK, &CCheckCaseNewDialog::OnBnClickedOk)
END_MESSAGE_MAP()


// CCheckCaseNewDialog 消息处理程序


BOOL CCheckCaseNewDialog::OnInitDialog()
{
	CDialogEx::OnInitDialog();

	// TODO:  在此添加额外的初始化
	PopulateModelCombo();

	return TRUE;  // return TRUE unless you set the focus to a control
				  // 异常: OCX 属性页应返回 FALSE
}

void CCheckCaseNewDialog::PopulateModelCombo()
{
	ck::CheckModel* pModel = NULL;
	ck::EnumCheckModel*	pModels = NULL;
	ck::CheckModelManager* pModelManager = ck::CheckModelManager::GetInstance();

	m_cmbModels.Clear();

	pModels = pModelManager->GetModels();
	pModels->Reset();
	while ((pModel = pModels->Next()))
	{
		int nItem = m_cmbModels.AddString(pModel->GetName());
		m_cmbModels.SetItemData(nItem, (DWORD)pModel->GetCode());
	}
	if (m_cmbModels.GetCount() > 0)
	{
		m_cmbModels.SetCurSel(0);
	}
}

void CCheckCaseNewDialog::OnBnClickedOk()
{
	// TODO: 在此添加控件通知处理程序代码
	UpdateData(TRUE);


	ck::CheckCaseManager* pCaseManager = ck::CheckCaseManager::GetInstance();

	z_uint32 code=1;
	const char* szCode = "";
	CString m_strDescription = "";
	z_uint32 nModelCode = (z_uint32)m_cmbModels.GetItemData(m_cmbModels.GetCurSel());

	//////////////////////////////////////////////////////////////////////////
	//1. 创建Case对象
	//////////////////////////////////////////////////////////////////////////
	RESULTCODE rc;
	rc = pCaseManager->CreateCase(m_strCaseName, code, szCode, nModelCode, m_strDescription);
	if (rc != RC_SUCCESS)
	{
		AfxMessageBox("创建方案失败");
		return;
	}
	ck::CheckCase* pCase = pCaseManager->GetCase(m_strCaseName);
	z_uint32 nCaseCode = pCase->GetCode();
	pCase->Release();

	//////////////////////////////////////////////////////////////////////////
	//2. 在Case对象上注册CheckItem
	//////////////////////////////////////////////////////////////////////////
	RegisterCheckItems(nModelCode, nCaseCode);

	//////////////////////////////////////////////////////////////////////////
	//3. 在Case对象上注册Operator
	//////////////////////////////////////////////////////////////////////////
	RegisterOperators(nModelCode, nCaseCode);	

	CDialogEx::OnOK();
}


RESULTCODE CCheckCaseNewDialog::RegisterCheckItems(z_uint32 modelCode, z_uint32 nCaseCode)
{
	ck::CheckModel* pModel = NULL;
	ck::CheckModelManager* pModelManager = ck::CheckModelManager::GetInstance();
	pModel = pModelManager->GetModel(modelCode);
	if (pModel == NULL)
	{
		AfxMessageBox("获取模型失败");
		return RC_FAILURE;
	}

	ck::CheckCaseManager* pCaseManager = ck::CheckCaseManager::GetInstance();

	z_uint32 nModelCode = pModel->GetCode();
	ck::EnumID* pCkItemCodes = pModelManager->GetCheckItemCodes(pModel->GetCode());
	if (pCkItemCodes)
	{
		z_uint32 nItemCode = 0;
		pCkItemCodes->Reset();
		while (!pCkItemCodes->IsEOF())
		{
			nItemCode = pCkItemCodes->Next();

			ck::EnumOperator* pOperators = pModelManager->GetOperators(nModelCode, nItemCode);
			if (pOperators)
			{
				ck::Operator* pOperator = NULL;
				pOperators->Reset();
				while ((pOperator = pOperators->Next()))
				{
					z_uint32 nOperCode = pOperator->GetCode();
					z_uint32 nModCode = pOperator->GetModule()->GetCode();
					pCaseManager->AddRule(nCaseCode, nItemCode, nOperCode, nModCode);
				}

				pOperators->Release();
			}
		}

		pCkItemCodes->Release();		
	}
	return RC_SUCCESS;
}

RESULTCODE CCheckCaseNewDialog::RegisterOperators(z_uint32 modelCode, z_uint32 nCaseCode)
{
	ck::CheckModel* pModel = NULL;
	ck::CheckModelManager* pModelManager = ck::CheckModelManager::GetInstance();
	pModel = pModelManager->GetModel(modelCode);
	if (pModel == NULL)
	{
		AfxMessageBox("获取模型失败");
		return RC_FAILURE;
	}

	ck::CheckCaseManager* pCaseManager = ck::CheckCaseManager::GetInstance();

	z_uint32 nModelCode = pModel->GetCode();
	ck::EnumID* pCkItemCodes = pModelManager->GetCheckItemCodes(pModel->GetCode());
	if (pCkItemCodes)
	{
		z_uint32 nItemCode = 0;
		pCkItemCodes->Reset();
		while (!pCkItemCodes->IsEOF())
		{
			nItemCode = pCkItemCodes->Next();

			ck::EnumOperator* pOperators = pModelManager->GetOperators(nModelCode, nItemCode);
			if (pOperators)
			{
				ck::Operator* pOperator = NULL;
				pOperators->Reset();
				while ((pOperator = pOperators->Next()))
				{
					z_uint32 nOperCode = pOperator->GetCode();
					z_uint32 nModCode = pOperator->GetModule()->GetCode();
					pCaseManager->AddRule(nCaseCode, nItemCode, nOperCode, nModCode);
				}

				pOperators->Release();
			}
		}

		pCkItemCodes->Release();
	}
	return RC_SUCCESS;
}
